package translate;

import Base.ResourceTest;
import com.huawei.translate.api.TranslateApi;
import com.huawei.translate.provider.HuaWeiTranslateProvider;
import com.huawei.translate.provider.MSTranslateProvider;
import com.huawei.translate.provider.YoudaoTranslateProvider;
import com.huawei.translate.vo.translate.TranslateCompareRequest;
import com.huawei.translate.vo.translate.TranslateCompareResponse;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.when;

@RunWith(SpringRunner.class)
public class When_request_translate_compare extends ResourceTest {

    @Autowired
    @Mock
    private MSTranslateProvider msTranslateProvider;

    @Autowired
    @Mock
    private YoudaoTranslateProvider youdaoTranslateProvider;

    @Autowired
    @Mock
    private HuaWeiTranslateProvider huaWeiTranslateProvider;

    @Autowired
    @InjectMocks
    private TranslateApi translateApi;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void should_return_translate_result() {

        when(msTranslateProvider.translate("en","en","hello","category")).thenReturn("microsoft");
        when(youdaoTranslateProvider.translate("en","en","hello","category")).thenReturn("youdao");
        when(huaWeiTranslateProvider.translate("en","en","hello","category")).thenReturn("huawei");

        TranslateCompareRequest request = BuildRequest();

        ResponseEntity<TranslateCompareResponse> responseEntity = this.testRestTemplate.postForEntity("/api/translates/compare", request, TranslateCompareResponse.class);

        TranslateCompareResponse response = responseEntity.getBody();

        Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        Assert.assertEquals(1,response.getMicrosoft().size());
        Assert.assertEquals(1,response.getYoudao().size());
        Assert.assertEquals(1,response.getHuawei().size());
        Assert.assertEquals("microsoft",response.getMicrosoft().get("category"));
        Assert.assertEquals("youdao",response. getYoudao().get("category"));
        Assert.assertEquals("huawei",response. getHuawei().get("category"));
    }

    private TranslateCompareRequest BuildRequest() {
        TranslateCompareRequest request = new TranslateCompareRequest();
        request.setFrom("en");
        request.setTo("en");
        request.setContent("hello");

        List<String> microsoft = new ArrayList<>();
        microsoft.add("category");

        List<String> youdao = new ArrayList<>();
        youdao.add("category");

        List<String> huawei = new ArrayList<>();
        huawei.add("category");
        request.setMicrosoft(microsoft);
        request.setYoudao(youdao);
        request.setHuawei(huawei);
        return request;
    }
}
